/*
 * @(#)Cache.java		       Project:ProgramList
 * Date:2012-8-22
 *
 * Copyright (c) 2011 CFuture09, Institute of Software, 
 * Guangdong Ocean University, Zhanjiang, GuangDong, China.
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.sinaapp.msdxblog.tools.htmlsax;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.sinaapp.msdxblog.tools.htmlsax.vo.Area;

/**
 * @author Geek_Soledad (66704238@51uc.com)
 */
public class Cache {
	// 缓存的根路径。
	private String cacheRootFile = null;
	private static Cache cache = null;

	private Cache() {
		cacheRootFile = "history";
		File file = new File(cacheRootFile);
		if (!file.exists()) {
			file.mkdirs();
		}
	}

	public static synchronized Cache getInstance() {
		if (cache == null) {
			cache = new Cache();
		}
		return cache;
	}

	/**
	 * 获取地区列表。
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Area> getAreas() {
		String path = cacheRootFile + File.separator + "areas";
		List<Area> areas = (List<Area>) readObject(Area.class, path);
		if (areas == null) {
			areas = TVHtmlParser.getAreas();
			writeObject(areas, path);
		}
		return areas;
	}

	private void writeObject(final List<?> list, final String path) {
		ObjectOutputStream oos = null;
		try {
			oos = new ObjectOutputStream(new FileOutputStream(path));
			oos.writeObject(list);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (oos != null) {
					oos.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

	@SuppressWarnings("unchecked")
	private List<?> readObject(Class<?> clazz, String path) {
		ObjectInputStream ois = null;
		if (clazz.equals(Area.class)) {
			try {
				ois = new ObjectInputStream(new FileInputStream(path));
				List<Area> areas = (List<Area>) ois.readObject();
				return areas;
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (ois != null) {
					try {
						ois.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
		return null;
	}

	public String getFilePath(String category, String url) {
		String uuid = UUID.nameUUIDFromBytes(url.getBytes()).toString();
		System.out.println(uuid.length() + uuid);
		return String.format("%s/%s-%s", getFileParentPath(), category, uuid);
	}

	private String getFileParentPath() {
		String date = SimpleDateFormat.getDateInstance().format(new Date());
		String path = String.format("%s/%s", cacheRootFile, date);
		File file = new File(path);
		file.mkdirs();
		return path;
	}

	/**
	 * 获取节目缓存文件的父路径
	 * 
	 * @return 节目的缓存文件的父路径，以日期为文件夹名称。
	 */
	public String getProgramFileParentPath() {
		String date = SimpleDateFormat.getDateInstance().format(new Date());
		String path = String.format("%s/%s", cacheRootFile, date);
		File file = new File(path);
		file.mkdirs();
		return file.getAbsolutePath();
	}

	/**
	 * 获取缓存根目录。
	 * 
	 * @return 缓存根目录。
	 */
	public String getCacheRootFile() {
		return cacheRootFile;
	}

}
